package com.cloudkinto.appletservice.statistics.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cloudkinto.appletservice.statistics.AppletStatisticsService;
import com.cloudkinto.common.DicBusinessItemConstant;
import com.cloudkinto.common.common.DateUtils;
import com.cloudkinto.common.utils.TimeUtils;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.*;
import com.cloudkinto.entity.*;
import com.cloudkinto.service.file.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangyy
 * @date 2020/6/19 9:26
 * @description:
 */
@Service
public class AppletStatisticsServiceImpl implements AppletStatisticsService {

    @Autowired
    private OrderOutputDao orderOutputDao;
    @Autowired
    private OrderAmazonDao orderAmazonDao;
    @Autowired
    private OrderRakutenDao orderRakutenDao;
    @Autowired
    private OrderQoo10Dao orderQoo10Dao;
    @Autowired
    private CompanyAccountDao companyAccountDao;
    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private FileService fileService;
    @Autowired
    private ExchangeRateDao rateDao;


    @Override
    public SingleResult appletStatistics(Long userId, Long companyId) {
        Map result = new HashMap();

        //昨日订单数量
        Date date = DateUtils.passingDate(new Date(), -1);
        Date dateYesStart = DateUtils.getStartOfDay(date);
        Date dateYesEnd = DateUtils.getEndOfDay(date);
        LambdaQueryWrapper<OrderOutputDo> yesWrapper = new LambdaQueryWrapper<>();
        yesWrapper.ge(OrderOutputDo::getCreateTime, dateYesStart)
                .lt(OrderOutputDo::getCreateTime, dateYesEnd)
                .eq(OrderOutputDo::getCompanyId, companyId);
        int yesTodayOrderCountTotal = orderOutputDao.selectCount(yesWrapper);
        yesWrapper.lt(OrderOutputDo::getStatus, DicBusinessItemConstant.outSendOut);
        int yesTodayOrderCountOut = orderOutputDao.selectCount(yesWrapper);
        result.put("yesTodayOrderCountTotal", yesTodayOrderCountTotal);
//        result.put("yesTodayOrderCountTotal", 132);
        result.put("yesTodayOrderCount", yesTodayOrderCountOut);
//        result.put("yesTodayOrderCount", 25);

        //今日订单数量
        Date nowDate = new Date();
        Date dateStart = DateUtils.getStartOfDay(nowDate);
        Date dateEnd = DateUtils.getEndOfDay(nowDate);
        LambdaQueryWrapper<OrderOutputDo> nowWrapper = new LambdaQueryWrapper<>();
        nowWrapper.ge(OrderOutputDo::getCreateTime, dateStart)
                .lt(OrderOutputDo::getCreateTime, dateEnd)
                .eq(OrderOutputDo::getCompanyId, companyId);
        int todayOrderCountTotal = orderOutputDao.selectCount(nowWrapper);
        nowWrapper.lt(OrderOutputDo::getStatus, DicBusinessItemConstant.outSendOut);
        int todayOrderCountOut = orderOutputDao.selectCount(nowWrapper);
        result.put("todayOrderCountTotal", todayOrderCountTotal);
//        result.put("todayOrderCountTotal", 141);
        result.put("todayOrderCount", todayOrderCountOut);
//        result.put("todayOrderCount", 170);

        //平台订单数
        LambdaQueryWrapper<OrderAmazonDo> amazonDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        amazonDoLambdaQueryWrapper.eq(OrderAmazonDo::getCompanyId, companyId);
        amazonDoLambdaQueryWrapper.gt(OrderAmazonDo::getPurchaseDate, TimeUtils.getCurrentDateFormat());
        int amazonCount = orderAmazonDao.selectCount(amazonDoLambdaQueryWrapper);

        LambdaQueryWrapper<OrderRakutenDo> rakutenDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        rakutenDoLambdaQueryWrapper.eq(OrderRakutenDo::getCompanyId, companyId);
        rakutenDoLambdaQueryWrapper.gt(OrderRakutenDo::getOrderDatetime, TimeUtils.getCurrentDateFormat());
        int rakutenCount = orderRakutenDao.selectCount(rakutenDoLambdaQueryWrapper);

        LambdaQueryWrapper<OrderQoo10Do> qoo10DoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        qoo10DoLambdaQueryWrapper.eq(OrderQoo10Do::getCompanyId, companyId);
        qoo10DoLambdaQueryWrapper.gt(OrderQoo10Do::getPaymentDate, TimeUtils.getCurrentDateFormat());
        int qooCount = orderQoo10Dao.selectCount(qoo10DoLambdaQueryWrapper);

        result.put("platformCount", amazonCount + rakutenCount + qooCount);
        result.put("amazonCount", amazonCount);
        result.put("rakutenCount", rakutenCount);
        result.put("qooCount", qooCount);
//        result.put("platformCount", 41175);
//        result.put("amazonCount", 15294);
//        result.put("rakutenCount", 25881);
//        result.put("qooCount", 0);

        return SingleResult.success(result);
    }

    @Override
    public SingleResult getCompanyAccount(Long userId, Long companyId) {
        JSONObject jsonObject = new JSONObject();
        UserDo userDo = userDao.selectById(userId);
        if (userDo != null) {
            jsonObject.put("username", userDo.getUserName() == null ? userDo.getUserAccount() : userDo.getUserName());
            jsonObject.put("headPic", fileService.getFilePath(userDo.getHeadFileId()));
        }
        LambdaQueryWrapper<CompanyAccountDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CompanyAccountDo::getCompanyId, companyId);
        CompanyAccountDo companyAccountDo = companyAccountDao.selectOne(wrapper);
        if (companyAccountDo != null) {
            jsonObject.put("companyAccount", companyAccountDo.getCompanyAccount());
        }
        CompanyDo companyDo = companyDao.selectById(companyId);
        if (companyDo != null) {
            jsonObject.put("companyCode", companyDo.getCompanyCode());
        }
        QueryWrapper wrapper1 = new QueryWrapper();
        wrapper1.orderByDesc("id");
        wrapper1.eq("type",DicBusinessItemConstant.currency_jpy);
        wrapper1.last("limit 1");
        ExchangeRateDo rateDo = rateDao.selectOne(wrapper1);
        if (rateDo != null)
            jsonObject.put("rate", rateDo.getRate());
        return SingleResult.success(jsonObject);
    }


}